package com.wdl.modules.myhouse.controller.sys;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wdl.modules.myhouse.common.Code;
import com.wdl.modules.myhouse.common.Result;
import com.wdl.modules.myhouse.entity.UserEntity;
import com.wdl.modules.myhouse.entity.sys.SysUserEntity;
import com.wdl.modules.myhouse.service.SysUserService;
import com.wdl.modules.myhouse.service.UserService;
import com.wdl.modules.myhouse.utils.util.PageUtil;
import com.wdl.modules.myhouse.vo.PageVO;
import com.wdl.modules.myhouse.vo.SysUserAddVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.method.P;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.web.bind.annotation.*;

import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author 昏昏的君
 */
@Api(tags = "系统用户")
@RequestMapping("/sysUser")
@RestController
@Slf4j
public class SysUserController {

    @Autowired
    private SysUserService sysUserService;




    @PostMapping("/update/{id}")
    @ApiOperation("根据用户id,更新用户信息")
    public Result updateById(@PathVariable("id")Integer id,@RequestBody SysUserAddVO vo){
        sysUserService.updateDetailById(id,vo);
        return new Result().ok();
    }

    @PostMapping("/save")
    @ApiOperation("保存用户")
    public Result save(@RequestBody SysUserAddVO addVO) {
        if (sysUserService.saveUserWithDetail(addVO)) {
            return new Result().ok();
        } else {
            return new Result().error();
        }
    }



    @DeleteMapping("/{id}")
    @ApiOperation("按照id删除元素")
    public Result del(@PathVariable("id") Integer id) {
        if (sysUserService.removeById(id)) {
            return new Result().ok();
        }
        return new Result().error();
    }


    @GetMapping("/allUser")
    public Result<Collection<Object>> allUser(PageVO pageVO) {
        Map<Integer, Object> map = sysUserService.getAllUserDetails();
        if (map != null) {
            Collection<Object> values = map.values();
            return new Result<Collection<Object>>().ok().setData(values);
        }
        return new Result<Collection<Object>>().error(Code.COMMON_ERROR.getCode(), "用户信息为空");
    }

    @GetMapping("/allUser/page")
    public Result<IPage> allUserByPage(PageVO pageVO) {
        IPage<Object> page = PageUtil.pageVo2Ipage(pageVO, new Object());
        Map<Integer, Object> map = sysUserService.getUserInfoByPage(pageVO);
        if (map != null) {
            Collection<Object> values = map.values();
            page.setRecords(values.stream().collect(Collectors.toList()));
        } else {
            return new Result<IPage>().error(Code.COMMON_ERROR.getCode(), "用户信息为空");
        }
        try {
            page.setTotal(sysUserService.getAllUserDetails().values().size());
        } catch (NullPointerException e) {
            log.error("分页查询用户数,用户为0");
            page.setTotal(0L);
        }
        return new Result<IPage>().ok().setData(page);
    }
}
